package com.platform.usercenter.teamfc.controller;

import com.platform.common.core.domain.R;
import com.platform.common.core.domain.entity.SysUser;
import com.platform.common.core.enums.MenuEndPointEnum;
import com.platform.common.core.utils.StringUtils;
import com.platform.usercenter.domain.SysMenuTreeDTO;
import com.platform.usercenter.service.ISysMenuService;
import com.platform.usercenter.teamfc.domain.dto.FuncRequest;
import com.platform.usercenter.teamfc.domain.dto.RouterDTO;
import com.platform.usercenter.teamfc.service.SysMenuFcService;
import com.platform.usercenter.utils.TreeAdapterUtil;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author twd
 * @date 2023/9/30 16:41
 * 菜单信息（远程调用）
 */
@RestController
@RequestMapping("/menuFc")
public class SysMenuFcController {


    @Resource
    private ISysMenuService menuService;
    @Resource
    private SysMenuFcService sysMenuFcService;


    @PostMapping("/selectUserMenuList")
    public R<List<SysMenuTreeDTO>> selectUserMenuList(@RequestBody SysUser sysUser) {
        Long userId = sysUser.getUserId();
        Long orgId = sysUser.getOrgId();
        List<SysMenuTreeDTO> menus = menuService.selectMenuTreeByUserIdRouter(userId, orgId, MenuEndPointEnum.PC.name());
        return R.ok(menus);
    }


    @PostMapping("/selectFuncList")
    public R<List<RouterDTO>> selectFuncList(@RequestBody FuncRequest funcRequest) {
        if (StringUtils.isEmpty(funcRequest.getHost()) || funcRequest.getUserId() == null || funcRequest.getOrgId() == null) {
            return R.fail("request param missing");
        }
        Long userId = funcRequest.getUserId();
        Long orgId = funcRequest.getOrgId();
        List<SysMenuTreeDTO> menus = menuService.selectMenuByUserId(userId, orgId, MenuEndPointEnum.PC.name());
        int menuId = Math.toIntExact(menus.stream()
                .filter(item -> item.getParentId() == 0).findFirst().orElse(new SysMenuTreeDTO()).getMenuId());
        menus = TreeAdapterUtil.buildTree(menus, SysMenuTreeDTO.class, null);
        List<RouterDTO> routerDTOList = sysMenuFcService.buildMenusPcNew(menus, funcRequest.getHost(), MenuEndPointEnum.PC.name(), (long) menuId);
        if (!CollectionUtils.isEmpty(routerDTOList)) {
            routerDTOList = routerDTOList.get(0).getChildren();
        }
        buildPath(routerDTOList);
        return R.ok(routerDTOList);
    }

    @PostMapping("/getUserFuncList")
    public R<List<RouterDTO>> selectMenuById(@RequestBody FuncRequest funcRequest) {
        Long[] ids = funcRequest.getMenuIds();
        String host = funcRequest.getHost();
        if (ids == null || ids.length < 1 || host == null) {
            return R.fail("request Param missing");
        }
        List<SysMenuTreeDTO> menus = sysMenuFcService.selectMenuByIds(ids);
        int menuId = Math.toIntExact(menus.stream()
                .filter(item -> item.getParentId() == 0).findFirst().orElse(new SysMenuTreeDTO()).getMenuId());
        return R.ok(sysMenuFcService.buildMenusPcNew(menus, funcRequest.getHost(), MenuEndPointEnum.PC.name(), (long) menuId));
    }

    private void buildPath(List<RouterDTO> routerDTOList) {
        for (RouterDTO routerDTO : routerDTOList) {
            String parentPath = routerDTO.getPath();
            List<RouterDTO> children = routerDTO.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                for (RouterDTO child : children) {
                    String path = child.getPath();
                    if (StringUtils.isNotEmpty(path)
                            && !StringUtils.startsWithAny(path, "http", "mailto", "tel:", "/")) {

                        String pathUrl = parentPath + "/" + path;
                        child.setPath(pathUrl.replaceAll("//", "/"));
                    }
                }
                buildPath(children);
            }
        }
    }
}
